﻿using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TracNghiemV1.Database;

namespace TracNghiemV1.DAO
{
    class CourseDao
    {
        private static TestDataDataContext m_dataContext = null;

        public static IList<SYSUSER> GetSysUserList()
        {
            return DataContextDao.GetInstance().SYSUSERs.ToList();
        }

        public static void AddCourse(COURSE newCourse)
        {

            m_dataContext = DataContextDao.GetInstance();

            m_dataContext.COURSEs.InsertOnSubmit(newCourse);
            m_dataContext.SubmitChanges();
        }

        public static void DeleteCourse(string courseId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var course = m_dataContext.COURSEs.Single(u => u.COURSE_ID == courseId);
            m_dataContext.COURSEs.DeleteOnSubmit(course);
            m_dataContext.SubmitChanges();
        }

        public static void ModifyCourse(COURSE modifiedCourse)
        {
            m_dataContext = DataContextDao.GetInstance();

            var course = m_dataContext.COURSEs.Single(u => u.COURSE_ID == modifiedCourse.COURSE_ID);

            course.COURSE_NAME = modifiedCourse.COURSE_NAME;
            course.COURSE_CREDIT = modifiedCourse.COURSE_CREDIT;

            m_dataContext.SubmitChanges();
        }

        public static COURSE FindCourseWithCourseId(string courseId)
        {
            m_dataContext = DataContextDao.GetInstance();
            return m_dataContext.COURSEs.Single(u => u.COURSE_ID == courseId);
        }

        public static IList<COURSE> FindCourseListWithName(string name)
        {
            m_dataContext = DataContextDao.GetInstance();

            var courses = from p in m_dataContext.COURSEs
                        where SqlMethods.Like(p.COURSE_NAME, "%" + name + "%")
                        select p;
            return courses.ToList();
        }

        public static COURSE GetLatestCourse()
        {
            m_dataContext = DataContextDao.GetInstance();
            return m_dataContext.COURSEs.ToList().Last();
        }
    }
}
